//跳转URL
var jumpurl_handle;
function jumpurl (url, time) {
	var timeout = time > 0 ? time * 1000 : 0;
	loading();
	$("#msgbox-div").remove();
	jumpurl_handle = setTimeout(function(){
		clearTimeout(jumpurl_handle);
		if(url){
			location.href = url;
		}else{
			history.go(-1);
		}
	}, timeout);
}
//生成随机密码
function suggest_password(id) {
	var pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
	var passwordlength = 16;
	var passwd = $('#'+id);
	passwdstr = '';
	for ( i = 0; i < passwordlength; i++ ) {
		passwdstr += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
	}
	passwd.val(passwdstr);
}
$(document).ready(function(){
	//链接点击事件
	$('a').live('click', function () {
		var url 	= $(this).attr('href');
			url 	= url ? url : '';
		var is_jump = true;
		var target  = $(this).attr('target');
			target  = target ? target : '';
		if($(this).hasClass('btn-confirm-opt')){is_jump = false;}
		if($(this).hasClass('fancybox-close')){is_jump = false;}
		if($(this).hasClass('noneAjax')){is_jump = false;}
		if($(this).hasClass('opt-tip')){is_jump = false;}
		if($(this).hasClass('nojump')){is_jump = false;}
		if($(this).hasClass('submit-form')){is_jump = false;}
		if($(this).hasClass('win')){is_jump = false;}
		if($(this).parent().hasClass('ajax-page')){is_jump = false;}
		if(target){is_jump = false;}
		if(url == '#'){is_jump = false;}

		if(is_jump && url){
			jumpurl(url);
			return false;
		}
	});

	//打印处理
	$.getScript("/Public/Js/jquery-plugin/jquery.jqprint-0.3.js",function () {
		$(".btn-print").click(function(){
			var container = $(this).attr('for');
				container = container ? '.' + container : 'body';
			$(container).jqprint();
		});
	});
	
	// 选择省份处理
	$('.ajax-province-change').live('change', function () {
		var request_url  = $(this).attr('url');
		var province_id  = $(this).val();
		var city_option  = '<option value="">-城市-</option>';
		var area_option  = '<option value="">-地区-</option>';
		var is_have_city = $(this).next('.ajax-city-change').size();
		$('.ajax-city-change').html(city_option).hide();
		$('.ajax-area-change').html(area_option).hide();
		if(!province_id || !request_url || !is_have_city){
			return false;
		}
		loading();
		$.ajax({
			url : request_url,
			data : 'parentid=' + province_id,
			type : 'get',
			dataType:'json',
			success : function(json){
				removeLoading();
				if(json.status == 1){
					var list = json.data.list;
					$.each(list, function(i){
						id   = list[i]['id'];
						name = list[i]['name'];
						city_option += '<option value="' + id + '">' + name + '</option>';
					});
					$('.ajax-city-change').html(city_option).show();
				}
			}
		});
	});
	// 选择城市处理
	$('.ajax-city-change').live('change', function () {
		var request_url  = $(this).attr('url');
		var city_id      = $(this).val();
		var area_option  = '<option value="">-地区-</option>';
		var is_have_area = $(this).next('.ajax-area-change').size();
		$('.ajax-area-change').html(area_option).hide();
		if(!city_id || !request_url || !is_have_area){
			return false;
		}
		loading();
		$.ajax({
			url : request_url,
			data : 'parentid=' + city_id,
			type : 'get',
			dataType:'json',
			success : function(json){
				removeLoading();
				if(json.status == 1){
					var list = json.data.list;
					$.each(list, function(i){
						id   = list[i]['id'];
						name = list[i]['name'];
						area_option += '<option value="' + id + '">' + name + '</option>';
					});
					$('.ajax-area-change').html(area_option).show();
				}
			}
		});
	});

	//提示信息JS
	$.getScript("/Public/Home/js/msgbox/msgbox.js",function () {
		$('.jump').live('click',function () {
			var url = $(this).attr('href');
			var url = url ? url : $(this).attr('url');
			jumpurl(url);
			return false;
		});
		//弹窗JS
		$.getScript("/Public/Home/js/win/win.js");
	});

	// 上传选中操作
	$('.win-upload-content .upload-img-box li').live('click', function () {
		var is_multi = $(this).attr('is-multi');
		if(!is_multi){
			$('.win-upload-content .upload-img-box li').removeClass('if-ok');
		}
		if($(this).hasClass('if-ok')){
			$(this).removeClass('if-ok');
		}else{
			$(this).addClass('if-ok');
		}
	});
	// 确定选中文件处理
	$('.confirm-selected-file').live('click', function () {
		var el = $(this).attr('to');
		var is_multi = $(this).attr('is-multi');
		var upload_field = $(this).attr('field');
		$('.win-upload-content .upload-img-box li.if-ok').each(function(){
			var file_id   = $(this).attr('file-id');
			var file_path = $(this).attr('file-path');
			var is_pic    = $(this).attr('is-pic');
			var html      = '<li><a class="nojump ' + (is_pic ? 'view_big_pic' : '') + '" href="' + file_path + '">' + $(this).html() + '</a><span class="cancel delete-select-upload-file" is-mulit="' + is_multi + '" field="' + upload_field + '">X</span></li>';
			if(!$('#' + el).find("input[value='" + file_id + "']").val()){
				if(is_multi){
					$('#' + el).append(html);
					$('.' + upload_field + '-upload-btn').show();
				}else{
					$('#' + el).html(html);
					$('.' + upload_field + '-upload-btn').hide();
				}
			}
			$.winRemove();
		});
	});
	$('.delete-select-upload-file').live('click', function () {
		var is_mulit = $(this).attr('is-mulit');
		var field    = $(this).attr('field');
		if(is_mulit == 0){
			$('.' + field + '-upload-btn').show();
		}
		$(this).parent().remove();
	});


	//加载更多
	$(".load-more").live('click', function () {
		loading();
		var here    = $(this);
		var url     = here.attr('url');
		var data_to = here.attr('data-to');
		if(!url){
			msgbox('URL未配置！', 'error');
		}
		$.ajax({
			type: "get",
			url: url,
			dataType:"json",
			success: function(data){
				removeLoading();
				if(data.status == 1){
					$("." + data_to).append(data.data.html);
					next_url = data.data.page.ajax_next_page;
					if(next_url){
						here.attr('url', next_url);
					}else{
						msgbox('到底了');
						here.remove();
					}
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});
	});
	// AJAX分页
	$('.ajax-page a').live('click', function () {
		var here    = $(this);
		var url     = here.attr('href');
			url     = url ? url : here.attr('url');
		var data_to = here.parent().attr('data-to');

		loading();
		$.ajax({
			type: "get",
			url: url,
			dataType:"json",
			success: function(data){
				removeLoading();
				if(data.status == 1){
					$("." + data_to).html(data.data.html);
					here.parent().html(data.data.page_html);
				}else{
					msgbox(data.info, 'error');
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});
		return false;
	});

	$('.label-select-box li').live('click', function () {
		if($(this).hasClass('on')){
			$(this).removeClass('on');
			$(this).find('input[type="checkbox"]').removeAttr('checked');
		}else{
			$(this).addClass('on');
			$(this).find('input[type="checkbox"]').attr('checked', 'checked');
		}
	});

	//星级评分
	$('.star-rating .pf_star').live('mousemove',function (e) {
		var offset    = $(this).offset().left;
		var curoffset = e.pageX - offset;
		var value     = (curoffset / $(this).width()) * 100;
		if(value <= 20){
			width = 20;
		}
		else if(value > 20 && value <= 40){
			width = 40;
		}
		else if(value > 40 && value <= 60){
			width = 60;
		}
		else if(value > 60 && value <= 80){
			width = 80;
		}else{
			width = 100;
		}
		// console.log(value+'=>'+width);

		$(this).find('.pf_star_on').width(width+'%');
		$(this).find('input').val(width / 20);
	});

	//查看大图
	$.getScript("/Public/Js/jquery-plugin/fancyBox/jquery.fancybox.pack.js?v=2.1.5",function () {
		$("<link>").attr({ 
			rel: "stylesheet",
			type: "text/css",
			href: "/Public/Js/jquery-plugin/fancyBox/jquery.fancybox.css?v=2.1.5"
		}).appendTo("head");
		//点击图片，查看大图
		$('.view_big_pic').fancybox();
	});
	//加载表单验证
	form_tip_type     = 3;//提示类型
	is_win_tip        = false;//提示弹窗
	is_show_all_error = true;//是否显示所有错误信息
	if($("form")){
		$.getScript("/Public/Js/validform/index.js",function () {
			//ajax表单提交
			$('form.ajax-form').live('submit',function () {
				if($('form.ajax-form').find('.Validform_wrong').html()){
					return false;
				}
				var url    = $(this).attr('action');
					url    = url ? url : window.location.href;
				var method = $(this).attr('method');
				var method = method ? method : 'post';
				var data   = $(this).serialize();

				loading('正在提交，请稍候...');
				$.ajax({
					url : url,
					data : data,
					type : method == 'post' ? 'post' : 'get',
					dataType:'json',
					success : function(json){
						removeLoading();
						if(json.status == 1){
							msgbox(json.info);
						}else{
							msgbox(json.info, 'error');
						}
						//有返回链接，则跳转
						if(json.url){
							jumpurl(json.url);
						}
					},
					error : function () {
						removeLoading();
						msgbox('请求失败，请稍候再试！', 'error');
					}
				});
				return false;
			});
		});
	}
	if($('.clipboard')){
		$.getScript("/Public/Js/ZeroClipboard/ZeroClipboard.min.js",function () {
			ZeroClipboard.config( { swfPath: "/Public/Js/ZeroClipboard/ZeroClipboard.swf" } );
			$('.clipboard').each(function () {
				var elem = $(this).attr('for');

				var client = new ZeroClipboard( $(this) );
				client.on( 'ready', function(event) {
					client.on( 'copy', function(event) {
						event.clipboardData.setData('text/plain', $('.' + elem).text());
					});

					client.on( 'aftercopy', function(event) {
						msgbox('复制成功！');
						// console.log('Copied text to clipboard: ' + event.data['text/plain']);
					});
				});

			});
		});
	}
	
	//操作提示
	$(".opt-tip").click(function(){
		var url = $(this).attr('tip-url');
		var loading_tip = $(this).attr('tip-loading');
		var tourl = $(this).attr('jumpurl');
		url = url ? url : $(this).attr('href');
		loading_tip = loading_tip ? loading_tip : '正在操作，请稍候...';
		if (!url) {
			return false;
		}
		loading(loading_tip);
		$.ajax({
			type: "get",
			url: url,
			dataType: "json",
			success : function(data){
				removeLoading();
				if(data.status == 1){
					msgbox(data.info);
					if(tourl){
						jumpurl(tourl);
					}
				}else{
					msgbox(data.info,'error');
				}
			},
			error :function (data) {
				removeLoading();
				msgbox('请求失败，请稍候再试！','error');
			}
		});
		return false;
	});
	//确认操作
	$(".btn-confirm-opt").live('click', function () {
		var checkbox = $(this);
		var url      = $(this).attr('url');
		var del_mod  = $(this).attr('del-class');
		var msg      = $(this).attr('msg');
		var method   = $(this).attr('method');

		url = url ? url : $(this).attr('href');
		msg = msg ? msg : '您确认要进行此操作吗？';

		html  = '<div style="width:300px;" class="confirm-opt-box">';
		html += '<div>' + msg + '</div>';
		html += '<div style="text-align:center;margin-top:8px"><span class="btn btn-confirm">确定</span> <span class="btn-cancel" onclick="$.winRemove()">取消</span></div>';
		html += '</div>';
		$.win_info('', html, '提示信息');
		$('.confirm-opt-box .btn-confirm').bind('click', function () {
			loading('正在处理，请稍候...');
			$.ajax({
				type: method ? method : 'get',
				url: url,
				dataType: 'json',
				success: function(data){
					removeLoading();
					$.winRemove();
					if(data.status == 1){
						msgbox(data.info);
						$('.' + del_mod).fadeOut('slow', function () {
							$('.' + del_mod).remove();
						});
						if(checkbox.attr('checked')){
							checkbox.removeAttr('checked');
						}else{
							checkbox.attr('checked', 'checked');
						}
						if(data.url){
							jumpurl(data.url);
						}
					}else{
						msgbox(data.info,'error');
					}
				},
				error : function () {
					removeLoading();
					msgbox('请求失败，请稍候再试！', 'error');
				}
			});
		})
		return false;
	});
	
	$('.submit-form').live('click', function () {
		var url        = $(this).attr('href');
		var method     = $(this).attr('method');
		var form_class = $(this).attr('form-class');
		var to_data    = $(this).attr('to-data');

		$('.' + form_class).wrap('<form class="tmp-submit-form-box"></form>');
		var form_data  = $('.tmp-submit-form-box').serialize();
		$('.' + form_class).unwrap();

		loading('正在处理，请稍候...');
		$.ajax({
			type: method ? method : 'post',
			url: url,
			data : form_data,
			dataType: 'json',
			success: function(data){
				removeLoading();
				if(data.status == 1){
					msgbox(data.info);
					if(data.url){
						jumpurl(data.url);
					}
					if(data.data.html && to_data){
						$('.' + to_data).html(data.data.html);
					}
				}else{
					msgbox(data.info, 'error');
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});
		return false;
	});

	var has_ueditor = $('body').has('#ueditor').html();
	if(has_ueditor != null){
		window.UEDITOR_HOME_URL = "/Public/Js/ueditor/";
		$.getScript("/Public/Js/ueditor/ueditor.config.js",function () {
			$.getScript("/Public/Js/ueditor/ueditor.all.min.js",function () {
				var ue = UE.getEditor('ueditor');
			});
		});
	}

	$('.share-box .if-weixin').mouseover(function () {
		$(this).prev('.weixin-share-box').stop().css({'opacity' : 1, 'bottom':'130%'});
	});
	$('.share-box .if-weixin').mouseleave(function () {
		$(this).prev('.weixin-share-box').stop().css({'opacity' : 0, 'bottom':'100%'});
	});
});